package com.lql.search.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.lql.api.client.ItemClient;
import com.lql.api.dto.ItemDTO;
import com.lql.search.domain.po.ItemDoc;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.jetbrains.annotations.NotNull;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

@Component
@Slf4j
@RequiredArgsConstructor
public class ItemListener {

    private final RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(HttpHost.create("http://192.168.44.128:9200"))
    );

    private final ItemClient itemClient;

    /**
     * 监听商品删除消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "search.item.delete.queue", durable = "true"),
            exchange = @Exchange(value = "search.direct", type = ExchangeTypes.DIRECT),
            key = "item.delete"
    ))
    public void listenItemDelete(Long id) throws IOException {
        log.info("监听商品删除消息：{}",id);
        // 1.准备request
        DeleteRequest request = new DeleteRequest("items", id.toString());
        // 2.发送请求
        client.delete(request, RequestOptions.DEFAULT);
        log.info("删除成功, 删除的商品id为{}",id);
    }

    /**
     * 监听商品新增消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "search.item.insert.queue", durable = "true"),
            exchange = @Exchange(value = "search.direct", type = ExchangeTypes.DIRECT),
            key = "item.insert"
    ))
    public void listenItemInsert(Long id) throws IOException {
        log.info("监听商品新增消息：{}",id);
        ItemDoc itemDoc = indexAndUpdate(id);
        log.info("新增成功, 新增商品信息为{}",itemDoc);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "search.item.update.queue", durable = "true"),
            exchange = @Exchange(value = "search.direct", type = ExchangeTypes.DIRECT),
            key = "item.update"
    ))
    public void listenItemUpdate(Long id) throws IOException {
        log.info("监听商品更新消息：{}",id);
        ItemDoc itemDoc = indexAndUpdate(id);
        log.info("更新成功, 更新商品信息为{}",itemDoc);

    }
    @NotNull
    private ItemDoc indexAndUpdate(Long id) throws IOException {
        // 1.准备request
        IndexRequest request = new IndexRequest("items", id.toString());
        // 2.准备请求参数
        ItemDTO itemDTO = itemClient.queryItemById(id);
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        // 设置更新时间
        itemDoc.setUpdateTime(LocalDateTime.now());

        request.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        // 3.发送请求
        client.index(request, RequestOptions.DEFAULT);
        return itemDoc;
    }
}
